package com.rimi.cs.javaweb.controller;

import com.rimi.cs.javaweb.pojo.Cart;
import com.rimi.cs.javaweb.pojo.Mobile;
import com.rimi.cs.javaweb.pojo.User;
import com.rimi.cs.javaweb.service.ICartService;
import com.rimi.cs.javaweb.service.IMobileService;
import com.rimi.cs.javaweb.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 手机相关操作的控制器
 *
 * @author Manka
 * @date 2018-06-28 08:48
 */
@Controller
public class MobilController {

    @Autowired
    private IMobileService mobileService;

    @Autowired
    private ICartService cartService;

    /**
     * 根据搜索关键字查询手机条目
     * @param searchItem 搜索关键字
     * @param model
     * @return
     */
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public String searchMobile(String searchItem,Model model){
        // 根据手机关键字查询手机条目
        List<Mobile> mobileList = mobileService.selectPhoneItems(searchItem);
        // 设置model属性 手机条目列表
        model.addAttribute("mobileList",mobileList);
        return "category";
    }

    /**
     * 增加购物车条目
     * @param pid 手机ID
     * @param quantity 数量
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/phone/addCart", method = RequestMethod.GET)
    public Map addCart(@RequestParam(value = "pid") Long pid, @RequestParam(value = "quantity", required = false, defaultValue = "1") Long quantity, HttpServletRequest request){
        Map<String,String> map = new HashMap(1);
        // 获取session属性 用户信息
        User user = (User) request.getSession().getAttribute(Constant.USER_INFO);
        if (user == null) {
            // 用户不存在
            map.put("addCartResult","false");
        } else {
            // 用户存在
            // 根据用户ID和手机ID查询购物车条目
            Cart cart = cartService.selectByUidAndPid(user.getUserid(),pid);
            if (cart != null) {
                // 购物车条目存在
                // 根据购物车条目ID更新购物车条目的数量
                cartService.updateItem(cart.getId(),cart.getQuantity()+quantity);
                map.put("addCartResult","true");
            } else {
                // 购物车条目不存在
                // 根据用户ID和手机ID插入购物车条目
                if (cartService.inseret(pid, quantity,request)) {
                    // 插入成功
                    // 更新session的属性 购物车条目总个数cartItemCount
                    Integer cartItemCount = (Integer) request.getSession().getAttribute("cartItemCount");
                    request.getSession().setAttribute("cartItemCount",cartItemCount + 1);
                    map.put("addCartResult","true");
                } else {
                    // 插入失败
                    map.put("addCartResult","false");
                }
            }
        }
        return map;
    }


    /**
     * 增加
     * @param pid 手机ID
     * @param request
     * @return
     */
    /*
    @ResponseBody
    @RequestMapping(value = "/phone/addLike", method = RequestMethod.GET)
    public Map addLike(Long pid, HttpServletRequest request){
        Map<String,String> map = new HashMap();
        Mobile mobile = mobileService.selectItemById(pid);
        if (mobile != null) {
            if (cartService.inseret(pid, request)) {
                map.put("addLikeResult","true");
            } else {
                map.put("addLikeResult","false");
            }
        } else {
            map.put("addLikeResult","notexist");
        }
        return map;
    }
    */
}
